#include "MySQLDb.h"

namespace SQLDoc
{
	const QString MySQLDb::SELECT_TABLES_QUERY("show tables");

	MySQLDb::MySQLDb(void)
		: DBDatabase()
	{ }

	MySQLDb::~MySQLDb(void)
	{ }

	void MySQLDb::fillTables()
	{
		QSqlDatabase db = QSqlDatabase::database(reinterpret_cast<DBConnection*>(this->m_parentItem)->connectionName());
		ensureActive(db);

		QSqlQuery query(db);
		query.setForwardOnly(true);
		query.exec(SELECT_TABLES_QUERY);

		while (query.next())
		{
			MySQLTable* t = new MySQLTable();
			t->setName(query.value(0).toString());
			m_tables.append(t);
		}

		DBDatabase::fillTables();
	}

	void MySQLDb::use(QSqlDatabase &db)
	{
		QSqlQuery query(db);
		query.exec(QString("use %1").arg(m_name));
		m_isActive = true;
	}
}
